<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>index</title>
 
<!-- Required Stylesheets -->
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet">
<link href="css/icheck/blue.css">

<!-- Required Javascript -->
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap/bootstrap-treeview.min.js"></script>
 <style>
       #tree{
          width:500px;
       }
    </style>
  </head>
  
  <body>
    <div id="tree"></div>
    <script type="text/javascript">
    var riddata;
    function saveRoleMenu(){
    	var checkedNodes = $('#tree').treeview('getChecked');
    	var menusStr="";
    	for(var i=0;i<checkedNodes.length;i++){
    		menusStr+=checkedNodes[i].tags[0]+",";
    	}
    	$.ajax({
    		url:"<%=request.getContextPath()%>/role/Role_updateRoleMenus",
    		data:{
    			menusStr:menusStr,
    			rid:riddata
    		},
    		dataType:"json",
    		success:function(value){
    				alert(value.message);
    		}
    	});
    }
    
    function showRoleMenuList(rid){
    	riddata = rid;
    	$.ajax({
    		url:"<%=request.getContextPath()%>/mmenu_getRoleMenuListToUpdate?rid="+rid,
    		success:function(data){
    			var tree = data;
    			$('#tree').treeview({data: tree,showCheckbox:true});//,enableLinks:true
    			$('#tree').treeview('expandAll', { levels: 2, silent: true });
    			var ignoreFlag=true;
    			$('#tree').on('nodeChecked', function(event, node) {
    				  //如果选中父节点，则将其下子节点全部选中
    				  if($('#tree').treeview('getParent', node).nodeId==undefined&&ignoreFlag){
    					  var firstChildNode = $('#tree').treeview('getNode', node.nodeId+1);
    					  $('#tree').treeview('checkNode',firstChildNode);
    					  var otherChildNodes = $('#tree').treeview('getSiblings', firstChildNode);
    					  for(var i = 0;i<otherChildNodes.length;i++){
    						  $('#tree').treeview('checkNode',otherChildNodes[i]); 
    					  }
    				  }
    				  if($('#tree').treeview('getParent', node).nodeId!=undefined){//如果选中子节点，则将其父节点选中
    					  ignoreFlag=false;
    					  var parentNode = $('#tree').treeview('getParent',node);
    					  $('#tree').treeview('checkNode',parentNode);
    					  ignoreFlag=true;
    				  }
    			}); 
    			$('#tree').on('nodeUnchecked', function(event, node) {
    				  //如果父节点取消，则将其下子节点全部取消
   					  var ifChildChecked=false;
    				  if($('#tree').treeview('getParent', node).nodeId==undefined&&ifChildChecked==false){
    					  var firstChildNode = $('#tree').treeview('getNode', node.nodeId+1);
    					  $('#tree').treeview('uncheckNode',firstChildNode);
    					  var otherChildNodes = $('#tree').treeview('getSiblings', firstChildNode);
    					  for(var i = 0;i<otherChildNodes.length;i++){
    						  $('#tree').treeview('uncheckNode',otherChildNodes[i]); 
    					  }
    				  }
    				  if($('#tree').treeview('getParent', node).nodeId!=undefined){
    					  //如果取消子节点，则检查兄弟节点，如果没有checked状态的兄弟节点，则取消其父节点
    					  var otherChildNodes = $('#tree').treeview('getSiblings', node);
    					  var checkedNodes = $('#tree').treeview('getChecked');
    					  for(var i=0;i<checkedNodes.length;i++){
    						  for(var j=0;j<otherChildNodes.length;j++){
	    						  if(checkedNodes[i].nodeId==otherChildNodes[j].nodeId){
	    							  ifChildChecked=true;
	    							  return;
	    						  }
    						  }
    					  }
    					  
    					  var parentNode = $('#tree').treeview('getParent',node);
    					  $('#tree').treeview('uncheckNode',parentNode);
    					  $('#tree').treeview('uncheckNode',node);
    				  } 
    			}); 
    		}
    	});
    }
   <%--  $(function(){
    	$.ajax({
    		url:"<%=request.getContextPath()%>/mmenu_getRoleMenuListToUpdate",
    		dataType:"text",
    		success:function(data){
    			var tree = data;
    			//alert(tree);
    			$('#tree').treeview({data: tree,showCheckbox:true});//,enableLinks:true
    			$('#tree').treeview('expandAll', { levels: 2, silent: true });
    		}
    	});
    });
 --%>
    </script>
    
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="button" onclick="saveRoleMenu()" class="btn btn-default">保存</button>
      <button type="button" onclick="location.href='local_product_listByPage.action'" class="btn btn-default">返回</button>
    </div>
  </div>
  </body>
  
</html>
